package Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import model.Contract;
import model.ContractRule;
import model.LeadRule;
import model.NullRule;
import model.OpportunityRule;
import model.Rule;


public class RuleRepository extends Repository {
	
//	Information information = new Information();
	static HashMap<Long,Rule> ruleMap = new HashMap<Long,Rule>();

	//private RuleRepository(){}
	
	static{
		Rule rule = new LeadRule();
		//Import only those lead which came after june 2009
		rule.setRuleDescripton("after june");
		rule.setRuleType("Lead");
		rule.setRuleName("Import");
		rule.setRuleID(1);
		ruleMap.put(new Long(1), rule);
		
		//Assign leads to those reps which have the same city name
		rule = new LeadRule();
		rule.setRuleDescripton("same city");
		rule.setRuleType("Lead");
		rule.setRuleName("Assign");
		rule.setRuleID(2);
		ruleMap.put(new Long(2), rule);
		
		//Merge leads which have the same name
		rule = new LeadRule();
		rule.setRuleDescripton("same name");
		rule.setRuleType("Lead");
		rule.setRuleName("Merge");
		rule.setRuleID(3);
		ruleMap.put(new Long(3), rule);
		
		//Prepare Lead as per customer choice
		rule = new LeadRule();
		rule.setRuleDescripton("Customer Interested");
		rule.setRuleType("Lead");
		rule.setRuleName("Prepare");
		rule.setRuleID(4);
		ruleMap.put(new Long(4), rule);
		
		//Approve only those leads whose product interest is  laptops
		rule = new LeadRule();
		rule.setRuleDescripton("laptop");
		rule.setRuleType("Lead");
		rule.setRuleName("Qualify");
		rule.setRuleID(5);
		ruleMap.put(new Long(5), rule);
		
		//import those opportunities whose interest is only dell laptop
		rule = new OpportunityRule();
		rule.setRuleDescripton("dell laptop");
		rule.setRuleType("Opportunity");
		rule.setRuleName("Import");
		rule.setRuleID(6);
		ruleMap.put(new Long(6), rule);
		
		rule = new OpportunityRule();
		rule.setRuleDescripton("non null");
		rule.setRuleType("Opportunity");
		rule.setRuleName("Prepare");
		rule.setRuleID(7);
		ruleMap.put(new Long(7), rule);
		
		//difference between the price in the Quote and ERP shud be less than 200
		rule = new OpportunityRule();
		rule.setRuleDescripton("difference less than 200");
		rule.setRuleType("Opportunity");
		rule.setRuleName("Approve");
		rule.setRuleID(8);
		ruleMap.put(new Long(8), rule);
		
		//close date of opportunity from current date is less than 30 days
		rule = new ContractRule();
		rule.setRuleDescripton("Closedate 30");
		rule.setRuleType("Contract");
		rule.setRuleName("Import");
		rule.setRuleID(9);
		ruleMap.put(new Long(9), rule);
		
		//All contract fields shud be non null
		rule = new ContractRule();
		rule.setRuleDescripton("non null");
		rule.setRuleType("Contract");
		rule.setRuleName("Prepare");
		rule.setRuleID(10);
		ruleMap.put(new Long(10), rule);
		
		//cost shud be greater than 50000
		rule = new ContractRule();
		rule.setRuleDescripton("2,Duration 2 months");
		rule.setRuleType("Contract");
		rule.setRuleName("Approve");
		rule.setRuleID(11);
		ruleMap.put(new Long(11), rule);		
				
	}
	
	public Object retrieve(String smsobjType,String name) {
		
		Collection<Rule> collectionRules = ruleMap.values();
		Rule r =  null;
		
		for(Rule rule : collectionRules){
			if(rule.getRuleType().equalsIgnoreCase(smsobjType) && rule.getRuleName().equalsIgnoreCase(name)){
				r =  rule;
				break;
			}
			
		}
		if(r == null){
			return new NullRule();
		}
		return r;
	}

	@Override
	public void delete(Long id) {
		ruleMap.remove(id);
		
	}

	@Override
	public void insert(Long id, Object obj) {
		ruleMap.put(id, (Rule)obj);
		
	}

	@Override
	public Object retrieve(Long id) {
		// TODO Auto-generated method stub
		return ruleMap.get(id);
	}

	@Override
	public void update(Long id, Object obj) {
		//ruleMap.remove(id);
		ruleMap.put(id, (Rule)obj);
		
	}

	@Override
	public List retrieve(String State) {
		return null;
	}

	@Override
	public boolean find(String objName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Collection retrieveAll()
	{
		Collection<Rule> listretrive=ruleMap.values();
		return listretrive;
	}
		
		
}
